/*
	This file is part of the OdinMS Maple Story Server
    Copyright (C) 2008 ~ 2010 Patrick Huy <patrick.huy@frz.cc> 
                       Matthias Butz <matze@odinms.de>
                       Jan Christian Meyer <vimes@odinms.de>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package tools.packet;

import java.sql.SQLException;
import java.sql.ResultSet;

import client.MapleClient;
import client.MapleCharacter;
import handling.MaplePacket;
import handling.SendPacketOpcode;
import tools.HexTool;
import tools.data.output.MaplePacketLittleEndianWriter;

public class MTSCSPacket {

  
	private static final byte[] warpCS = HexTool.getByteArrayFromHexString("00 00 00 63 00 74 00 65 00 64 00 2F 00 32 00 00 00 00 00 02 00 11 00 AD 01 08 06 02 00 00 00 33 00 00 00 05 00 13 00 AF 00 08 06 A0 01 14 00 30 E1 7B 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 00 18 00 B2 01 08 06 02 00 00 00 33 00 00 00 08 00 1A 00 B4 00 0A 06 B8 01 14 00 A8 10 88 06 69 00 6C 00 6C 00 2F 00 39 00 30 00 30 00 31 00");
    public static MaplePacket warpCS(MapleClient c) {
	final MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();

	mplew.writeShort(SendPacketOpcode.CS_OPEN.getValue());

	final MapleCharacter chr = c.getPlayer();

	mplew.writeLong(-1);
	mplew.write(0);
	PacketHelper.addCharStats(mplew, chr);
	mplew.writeShort(chr.getBuddylist().getCapacity());
	PacketHelper.addInventoryInfo(mplew, chr);
	mplew.writeZeroBytes(16);
	PacketHelper.addRocksInfo(mplew, chr);
	mplew.writeZeroBytes(15);
	mplew.write(warpCS);
	for (int i = 0; i <= 8; i++) {
		for (int j = 0; j <= 1; j++) {
			for (int x = 0; x < 5; x++) {
				mplew.writeInt(i);
				mplew.writeInt(j);
				mplew.writeInt(0); //SN ID
			}
		}
	}
	mplew.writeZeroBytes(9);

	return mplew.getPacket();
    
    }

    public static MaplePacket useCharm(byte charmsleft, byte daysleft) {
	MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();

	mplew.writeShort(SendPacketOpcode.SHOW_ITEM_GAIN_INCHAT.getValue());
	mplew.write(6);
	mplew.write(1);
	mplew.write(charmsleft);
	mplew.write(daysleft);

	return mplew.getPacket();
    }

    public static MaplePacket itemExpired(int itemid) {
	MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
	// 1E 00 02 83 C9 51 00
	
	// 21 00 08 02 
	// 50 62 25 00
	// 50 62 25 00
	mplew.writeShort(SendPacketOpcode.SHOW_STATUS_INFO.getValue());
	mplew.write(2);
	mplew.writeInt(itemid);

	return mplew.getPacket();
    }

    public static MaplePacket ViciousHammer(boolean start, int hammered) {
	MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();

	mplew.writeShort(SendPacketOpcode.VICIOUS_HAMMER.getValue());
	if (start) {
	    mplew.write(49);
	    mplew.writeInt(0);
	    mplew.writeInt(hammered);
	} else {
	    mplew.write(53);
	    mplew.writeInt(0);
	}

	return mplew.getPacket();
    }

    public static MaplePacket changePetName(MapleCharacter chr, String newname, int slot) {
	MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
	mplew.writeShort(SendPacketOpcode.PET_NAMECHANGE.getValue());

	mplew.writeInt(chr.getId());
	mplew.write(0);
	mplew.writeMapleAsciiString(newname);
	mplew.write(0);

	return mplew.getPacket();
    }

    public static MaplePacket showNotes(ResultSet notes, int count) throws SQLException {
	MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();

	mplew.writeShort(SendPacketOpcode.SHOW_NOTES.getValue());
	mplew.write(3);
	mplew.write(count);
	for (int i = 0; i < count; i++) {
	    mplew.writeInt(notes.getInt("id"));
	    mplew.writeMapleAsciiString(notes.getString("from"));
	    mplew.writeMapleAsciiString(notes.getString("message"));
	    mplew.writeLong(PacketHelper.getKoreanTimestamp(notes.getLong("timestamp")));
	    mplew.write(0);
	    notes.next();
	}

	return mplew.getPacket();
    }

    public static MaplePacket useChalkboard(final int charid, final String msg) {
	MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
	mplew.writeShort(SendPacketOpcode.CHALKBOARD.getValue());

	mplew.writeInt(charid);
	if (msg == null) {
	    mplew.write(0);
	} else {
	    mplew.write(1);
	    mplew.writeMapleAsciiString(msg);
	}

	return mplew.getPacket();
    }

    public static MaplePacket getTrockRefresh(MapleCharacter chr, boolean vip, boolean delete) {
	MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();

	mplew.writeShort(SendPacketOpcode.TROCK_LOCATIONS.getValue());
	mplew.write(delete ? 2 : 3);
	mplew.write(vip ? 1 : 0);
	int[] map = chr.getRocks();
	for (int i = 0; i < 10; i++) {
	    mplew.writeInt(map[i]);
	}
	return mplew.getPacket();
    }

    public static MaplePacket sendWishList(MapleCharacter chr, boolean update) {
	MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();

	mplew.writeShort(SendPacketOpcode.CS_OPERATION.getValue());
	mplew.write(update ? 0x58 : 0x52);
	int[] list = chr.getWishlist();
	for (int i = 0; i < 10; i++) {
	    mplew.writeInt(list[i] != -1 ? list[i] : 0);
	}
	return mplew.getPacket();
    }

    public static MaplePacket showNXMapleTokens(MapleCharacter chr) {
	MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();

	mplew.writeShort(SendPacketOpcode.CS_UPDATE.getValue());
	mplew.writeInt(chr.getCSPoints(1)); // A-cash
	mplew.writeInt(chr.getCSPoints(2)); // MPoint

	return mplew.getPacket();
    }

    public static MaplePacket showBoughtCSItem(int itemid) {
	MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();

	mplew.writeShort(SendPacketOpcode.CS_OPERATION.getValue());
	mplew.writeInt(1394677841); // Probably CS item serial number
	mplew.writeZeroBytes(5);
	mplew.writeInt(1015452); // 9C 7E 0F 00
	mplew.writeZeroBytes(4);
	mplew.writeInt(itemid);
	mplew.write(HexTool.getByteArrayFromHexString("D5 D0 CC 01 01 00 00 0A 00 00 00 00 04 FD AE 0A 90 82 82 60 AE FB DE 24 C1 CA 01"));
	mplew.writeZeroBytes(8);

	return mplew.getPacket();
    }

    public static MaplePacket showBoughtCSQuestItem(short position, int itemid) {
	MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();

	mplew.writeShort(SendPacketOpcode.CS_OPERATION.getValue());
	mplew.writeInt(381);
	mplew.write(0);
	mplew.writeShort(1);
	mplew.write(position);
	mplew.write(0);
	mplew.writeInt(itemid);

	return mplew.getPacket();
    }

    public static MaplePacket wrongCouponCode() {
	MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();

	mplew.writeShort(SendPacketOpcode.CS_OPERATION.getValue());
	mplew.write(0x40);
	mplew.write(0x87);

	return mplew.getPacket();
    }

    public static MaplePacket showCouponRedeemedItem(int itemid) {
	MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();

	mplew.writeShort(SendPacketOpcode.CS_OPERATION.getValue());
	mplew.writeShort(0x3A);
	mplew.writeInt(0);
	mplew.writeInt(1);
	mplew.writeShort(1);
	mplew.writeShort(0x1A);
	mplew.writeInt(itemid);
	mplew.writeInt(0);

	return mplew.getPacket();
    }

    public static MaplePacket enableUse0() {
	MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();

	mplew.writeShort(0x12);
	mplew.write(1);
	mplew.writeInt(0);

	return mplew.getPacket();
    }

    public static MaplePacket enableUse1() {
	MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();

	mplew.writeShort(SendPacketOpcode.CS_OPERATION.getValue());
	mplew.writeShort(0x40);
	mplew.write(0);
	mplew.writeShort(4);
	mplew.writeShort(4);

	return mplew.getPacket();
    }

    public static MaplePacket enableUse2() {
	MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();

	mplew.writeShort(SendPacketOpcode.CS_OPERATION.getValue());
	mplew.write(0x42);
	mplew.writeShort(0);

	return mplew.getPacket();
    }

    public static MaplePacket enableUse3() {
	MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();

	mplew.writeShort(SendPacketOpcode.CS_OPERATION.getValue());
	mplew.write(44);
	mplew.write(58);
	mplew.write(0x9F);
	mplew.write(98);
	mplew.writeZeroBytes(37);

	return mplew.getPacket();
    }
}
